<script setup name="Product">
import { ref, reactive, defineEmits } from 'vue'
import { GetProductAPI } from '@/api/productionTube/common.js'
const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset'])
const props = defineProps({
  isShow: {
    type: Boolean,
    default: false
  }
})
const loading = ref(false)

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  materialCode: '', // 产品编码
  materialName: '', // 产品名称
  publicNumber: '' // 制令单号
})

const handleSelect = ref({ publicNumber: '' })
const TableData = ref([])
const total = ref(0)

// 勾选数据
const handleSelectionChange = row => {
  handleSelect.value = row
}
// 是否禁用保存
const d = computed(() => {
  return handleSelect.value?.publicNumber ? false : true
})

// 提交查询
const Submit = () => {
  emit('selectsubmit', handleSelect.value)
}

// 提交
const preservation = () => {
  emit('preservation', handleSelect.value)
}

// 重置
const reset = () => {
  query.materialCode = ''
  query.supplierName = ''
  getList()
}

// 查询
const getList = async () => {
  try {
    loading.value = true
    const res = await GetProductAPI(query)
    total.value = res.total
    TableData.value = res.data
    loading.value = false
  } catch {
  } finally {
    loading.value = false
  }
}
getList()
</script>
<template>
  <div class="app-container">
    <p-XiaoheDialog :dialogVisible="props.isShow" title="产品信息" @close="emit('cancel')" width="50%">
      <template #body>
        <!-- 查询条件 -->
        <el-form :inline="true" label-width="100px">
          <el-form-item label="产品编码">
            <el-input clearable v-model.trim="query.materialCode" placeholder="输入产品编码" />
          </el-form-item>
          <el-form-item label="产品名称">
            <el-input clearable v-model.trim="query.materialName" placeholder="输入产品名称" />
          </el-form-item>
          <el-form-item label="制令单号">
            <el-input clearable v-model.trim="query.publicNumber" placeholder="输入制令单号" />
          </el-form-item>
        </el-form>
        <el-divider />
        <el-table
          stripe
          border
          v-if="!loading"
          v-loading="loading"
          :data="TableData"
          highlight-current-row
          @current-change="handleSelectionChange"
        >
          <el-table-column label="序号" width="80" type="index" />
          <el-table-column width="80">
            <template #default="scope">
              <el-radio-group v-model="handleSelect.publicNumber">
                <el-radio :label="scope.row.publicNumber" size="large">&nbsp;</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column label="制令单号" prop="publicNumber" width="auto" />
          <el-table-column label="产品编码" prop="materialCode" width="auto" />
          <el-table-column label="产品名称" prop="materialName" width="auto" />
          <el-table-column label="产品规格" prop="materialSpec" width="auto" />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="query.pageNum"
          v-model:limit="query.pageSize"
          @pagination="getList"
        />
        <!-- 按钮 -->
        <div>
          <el-row class="Btn">
            <el-button type="primary" @click="getList">查询列表</el-button>
            <el-button type="info" @click="reset">重置</el-button>
            <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
            <el-button type="info" @click="emit('cancel')">取消</el-button>
          </el-row>
        </div>
      </template>
    </p-XiaoheDialog>
  </div>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
</style>
